####################################
# global libraries used everywhere #
####################################
pkgTest <- function(x,y="")
{
  if (!require(x,character.only = TRUE))
  {
    if ( y == "" ) 
    {
      install.packages(x,dep=TRUE)
    } else {
      remotes::install_version(x, y)
    }
    if(!require(x,character.only = TRUE)) stop("Package not found")
  }
  return("OK")
}

pkgTestGithub <- function(x)
{
  pkg <- sub(".*/", "", x)
  if (x == "markwestcott34/stargazer-booktabs")
  {
    pkg <- sub("-.*", "", pkg)
  }
  if (!require(pkg,character.only = TRUE))
  {
    install_github(x,force=TRUE)
    if(!require(pkg,character.only = TRUE)) stop("Package not found")
  }
  return("OK")
}

global.libraries <- c("tidyverse", "haven", "ggsignif", "lubridate", "showtext", "broom", "weights",
                      "extrafont", "stringr", "ggpubr", "zoo", "anesrake", "RColorBrewer", "multcomp",
                      "fixest", "gridExtra", "sf", "devtools", "ggplot2")

global.libraries.github <- c("ChandlerLutz/starpolishr", "markwestcott34/stargazer-booktabs")


results <- sapply(as.list(global.libraries), pkgTest)
results.github <- sapply(as.list(global.libraries.github), pkgTestGithub)
